package com.aat.dxfy.redis;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.SortParameters.Order;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.query.SortQueryBuilder;
import org.springframework.stereotype.Repository;

@Repository
public class RedisDao {
	@Autowired
	private RedisTemplate<String, Object> redisTemplate;

	public void addKV(String k, Object v) {
		redisTemplate.opsForValue().set(k, v);
	}

	public void delKV(String k) {
		redisTemplate.opsForValue().getOperations().delete(k);
	}

	public Object getKV(String k) {
		return redisTemplate.opsForValue().get(k);
	}

	// 增加限时
	public void addKV(String k, Object v, Long offset) {
		redisTemplate.opsForValue().set(k, v, offset);
	}

	// SET LIST
	public Object addListLeft(String k, Object v) {
		return redisTemplate.opsForList().leftPush(k, v);
	}

	public Object addListRight(String k, Object v) {
		return redisTemplate.opsForList().rightPush(k, v);
	}

	public void countplus(String k) {
		redisTemplate.watch(k);
		Object o = redisTemplate.opsForValue().get(k);
		if (o == null) {
			redisTemplate.opsForValue().set(k, 0L);
		} else {
			Long l = (Long) o;
			l += 1;
			redisTemplate.opsForValue().set(k, l);
		}
		redisTemplate.unwatch();
	}

	public Object addListLeftAll(String k, List<Object> values) {
		return redisTemplate.opsForList().leftPushAll(k, values);
	}

	public Object addListRightAll(String k, List<Object> values) {
		return redisTemplate.opsForList().rightPushAll(k, values);
	}

	public List<Object> getList(String k, long start, long end) {
		return redisTemplate.opsForList().range(k, start, end);
	}

	public List<Object> sortPageList(String key, String subKey, String by, boolean isDesc, boolean isAlpha, int off,
			int num) throws Exception {
		SortQueryBuilder<String> builder = SortQueryBuilder.sort(key);
		builder.by(subKey + "*->" + by);
		builder.get("#");
		builder.alphabetical(isAlpha);

		if (isDesc) {
			builder.order(Order.DESC);
		}

		builder.limit(off, num);

		List<Object> cks = redisTemplate.sort(builder.build());
		List<Object> result = new ArrayList<Object>();
		for (Object ck : cks) {
			// 得到项目对象 by(subKey+ck);
			result.add(ck);
		}

		return result;
	}

	public boolean addSortSet(String key, Object v, Double weightScore) {
		return redisTemplate.boundZSetOps(key).add(v, weightScore);
	}

	public boolean addSortSet(String key, Object v, Double weightScore, Date endDate) {
		redisTemplate.boundZSetOps(key).expireAt(endDate);
		return redisTemplate.boundZSetOps(key).add(v, weightScore);
	}

	public Set<Object> getSortSet(String key, String order) {
		if ("DESC".equalsIgnoreCase(order)) {
			return redisTemplate.boundZSetOps(key).reverseRange(0, -1);
		} else {
			return redisTemplate.boundZSetOps(key).range(0, -1);
		}
	}

	public List<Object> sortList(String key, String subKey, String by, boolean isDesc, boolean isAlpha, int off,
			int num) throws Exception {
		SortQueryBuilder<String> builder = SortQueryBuilder.sort(key);
		builder.by(subKey + "*->" + by);
		builder.get("#");
		builder.alphabetical(isAlpha);

		if (isDesc) {
			builder.order(Order.DESC);
		}

		builder.limit(off, num);

		List<Object> cks = redisTemplate.sort(builder.build());
		List<Object> result = new ArrayList<Object>();
		for (Object ck : cks) {
			// 得到项目对象 by(subKey+ck);
			result.add(ck);
		}

		return result;
	}

}
